//
//  ServiceCell.m
//  ProjectManager
//
//  Created by Lssyyy on 2019/3/28.
//  Copyright © 2019年 EIntone. All rights reserved.
//

#import "ServiceCell.h"
#define kChatCellItemMargin 10.f
#define kChatCellIconImageViewWH 35.f

#define kLabelMargin 20.f
#define kLabelTopMargin 8.f
#define kLabelBottomMargin 20.f

#define kMaxContainerWidth 220.f
@interface ServiceCell()
@property (nonatomic, strong) UIView *container;
@property (nonatomic, strong) UIImageView *containerBackgroundImageView;
@property (nonatomic,strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UIImageView *maskImageView;
@end

@implementation ServiceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupView];
    }
    return self;
}
- (void)setupView{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _iconImageView = [UIImageView new];
    [self.contentView addSubview:_iconImageView];
    
    _container = [UIView new];

    [self.contentView addSubview:self.container];
    
    [_container addSubview:self.contentLabel];
    
    _containerBackgroundImageView = [UIImageView new];
    [_container insertSubview:_containerBackgroundImageView atIndex:0];
    
     _maskImageView = [UIImageView new];
    
     [self setupAutoHeightWithBottomView:_container bottomMargin:0];
    
    // 设置containerBackgroundImageView填充父view
_containerBackgroundImageView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
}

- (void)setModel:(ServiceModel *)model{
    _model = model;
    _contentLabel.text = model.CSC_NAME;
    self.iconImageView.image = [UIImage imageNamed:model.iconName];
    
    // 根据model设置cell左浮动或者右浮动样式
    [self setMessageOriginWithModel:model];
    
    // 清除展示图片时候用到的mask
    [_container.layer.mask removeFromSuperlayer];
    
    // 清除展示图片时候_containerBackgroundImageView用到的didFinishAutoLayoutBlock
    _containerBackgroundImageView.didFinishAutoLayoutBlock = nil;
    
    _contentLabel.sd_layout
    .leftSpaceToView(_container, kLabelMargin)
    .topSpaceToView(_container, kLabelTopMargin)
    .autoHeightRatio(0); // 设置label纵向自适应
    
    // 设置label横向自适应
    [_contentLabel setSingleLineAutoResizeWithMaxWidth:kMaxContainerWidth];
    
    // container以label为rightView宽度自适应
    [_container setupAutoWidthWithRightView:_contentLabel rightMargin:kLabelMargin];
    
    // container以label为bottomView高度自适应
    [_container setupAutoHeightWithBottomView:_contentLabel bottomMargin:kLabelBottomMargin];
}

- (void)setMessageOriginWithModel:(ServiceModel *)model
{
    if (model.messageType == MessageTypeSend) {
        // 发出去的消息设置居右样式
        self.iconImageView.sd_resetLayout
        .rightSpaceToView(self.contentView, kChatCellItemMargin)
        .topSpaceToView(self.contentView, kChatCellItemMargin)
        .widthIs(kChatCellIconImageViewWH)
        .heightIs(kChatCellIconImageViewWH);
        
        _container.sd_resetLayout.topEqualToView(self.iconImageView).rightSpaceToView(self.iconImageView, kChatCellItemMargin);
        
        _containerBackgroundImageView.image = [[UIImage imageNamed:@"SenderTextNodeBkg"] stretchableImageWithLeftCapWidth:50 topCapHeight:30];
    } else if (model.messageType == MessageTypeReceive) {
        
        // 收到的消息设置居左样式
        self.iconImageView.sd_resetLayout
        .leftSpaceToView(self.contentView, kChatCellItemMargin)
        .topSpaceToView(self.contentView, kChatCellItemMargin)
        .widthIs(kChatCellIconImageViewWH)
        .heightIs(kChatCellIconImageViewWH);
        
        _container.sd_resetLayout.topEqualToView(self.iconImageView).leftSpaceToView(self.iconImageView, kChatCellItemMargin);
        
        _containerBackgroundImageView.image = [[UIImage imageNamed:@"ReceiverTextNodeBkg"] stretchableImageWithLeftCapWidth:50 topCapHeight:30];
    }
    
    _maskImageView.image = _containerBackgroundImageView.image;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [UILabel new];
        _contentLabel.text = @"123";
        _contentLabel.textColor = UIColorWithRGB16Radix(0x333333);
        _contentLabel.font = [UIFont systemFontOfSize:13];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}
@end
